<template>
  <div>
    <!-- 导入 -->
    <upload-excel :before-upload="beforeUpload" :on-success="onSuccess" />
  </div>
</template>

<script>
import { importEmployee } from '@/api/employee'
import { getImportData } from '@/utils/excelData'
export default {
  name: 'Uploadexcel',
  methods: {
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      // 小于1M
      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    // excel解析完毕自动调用的回调函数
    async onSuccess({ results, header }) {
      console.log(results)
      // 导入时的处理之后的接口
      const data = getImportData(results)
      // 导入excel
      await importEmployee(data)
      this.$router.back()
    }
  }
}

</script>

<style>

</style>
